package com.example.a17376080_dlz_finaltest;

import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;


import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import adapter.Comic;
import adapter.MajorBc;
import adapter.MajorBcAdapter;
import okhttp3.Call;
import okhttp3.Response;
import utils.HttpUtil;


public class BrowseCartoonsActivity extends AppCompatActivity {
    ImageView bc_view;
    int position;
    Button btn_top,btn_bottom;
    MajorBcAdapter adapter;
    private List<MajorBc> majorBcList=new ArrayList<>();
    private static final int UPDATA_TXT=1;
String uri="http://106.14.221.71:8080/comic/hzw/0";
int index=3;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what){
                case UPDATA_TXT:
                   adapter.notifyDataSetChanged();
                    break;

            }
        }
    };
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_browse_cartoons);
        Intent intent=getIntent();
       position=intent.getIntExtra("position",00);
        Toast.makeText(this,"位置是："+position,Toast.LENGTH_LONG).show();
        intiView();
        RecyclerView recyclerView=findViewById(R.id.recycle_bcview);
        LinearLayoutManager layoutManager=new LinearLayoutManager(this);
        recyclerView.setLayoutManager(layoutManager);
        adapter=new MajorBcAdapter(majorBcList,getApplicationContext());

        //设置缓存
        recyclerView.setItemViewCacheSize(17);

        recyclerView.setAdapter(adapter);
      //  if (position==0){
            if(majorBcList==null||majorBcList.isEmpty()){
                startOkHttp();
          ///  }
        }


        //intEvent();
    }

    private void startOkHttp() {
        HttpUtil.sendOkHttpRequest("http://106.14.221.71:8080/Service_dlz/comicBc_findAll.action?comic_id.comic_id="+position,new okhttp3.Callback(){
            @Override
            public void onResponse(Call call, Response response) throws IOException {
                String responseData=response.body().string();
                Log.d("回来的数据：", responseData);
                //GSON解析
                parseJSONWithGSON(responseData);
                Message message=new Message();
                message.what=UPDATA_TXT;
                handler.sendMessage(message);

            }

            @Override
            public void onFailure(Call call, IOException e) {
                Log.d("没有得到信息", "onFailure: ");
            }
        });

    }
    private void parseJSONWithGSON(String jsonData) {
        Gson gson=new Gson();
        majorBcList.addAll((Collection<? extends MajorBc>) gson.fromJson(jsonData,new TypeToken<List<MajorBc>>(){}.getType()));


    }

    private void intiView() {
        bc_view=findViewById(R.id.bc_view);
    }



}
